package com.maxlen.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.utils.Constant;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.coupon.dao.SeckillSkuRelationDao;
import com.maxlen.coupon.entity.SeckillSessionEntity;
import com.maxlen.coupon.entity.SeckillSkuRelationEntity;
import com.maxlen.coupon.service.SeckillSkuRelationService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;


@Service("seckillSkuRelationService")
public class SeckillSkuRelationServiceImpl extends ServiceImpl<SeckillSkuRelationDao, SeckillSkuRelationEntity> implements SeckillSkuRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<SeckillSkuRelationEntity> wrapper = new LambdaQueryWrapper<>();
        System.out.println(params);
        if(params.get(Constant.KEY) != null && StringUtils.hasText(params.get(Constant.KEY).toString())){
            wrapper
                    .like(SeckillSkuRelationEntity::getId, params.get(Constant.KEY))
                    .or()
                    .like(SeckillSkuRelationEntity::getPromotionSessionId, params.get(Constant.KEY))
                    .or()
                    .like(SeckillSkuRelationEntity::getSkuId, params.get(Constant.KEY));
        }

        if(params.get(Constant.PROMOTION_SESSION_ID) != null && StringUtils.hasText(params.get(Constant.PROMOTION_SESSION_ID).toString())){
            wrapper.eq(SeckillSkuRelationEntity::getPromotionSessionId, params.get(Constant.PROMOTION_SESSION_ID));
        }

        IPage<SeckillSkuRelationEntity> page = this.page(
                new Query<SeckillSkuRelationEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}
